﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using ICSharpCode.TextEditor;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace DevelopTool.Forms.CommonControls.CodeEditor
{
    /// <summary>
    /// 文本编辑器设置窗体
    /// </summary>
    public partial class CodeEditorSettingForm : BaseUIForm
    {
        private TextEditorControl textEditorControl;

        private readonly FontDialog fontDialog = new FontDialog();

        public CodeEditorSettingForm(TextEditorControl textEditorControl)
        {
            InitializeComponent();
            this.textEditorControl = textEditorControl;
        }

        private void TextEditorSettingForm_Load(object sender,EventArgs e)
        {
            #region 通过配置设置控件状态
            string fontFamily = Setting.TextEditor.FontFamily;
            float fontSize = Setting.TextEditor.FontSize;
            string fontStyle = Setting.TextEditor.FontStyle;
            if(!string.IsNullOrWhiteSpace(fontFamily) && fontSize > 0)
            {
                fontDialog.Font = new Font(fontFamily,fontSize,(FontStyle)Enum.Parse(typeof(FontStyle),fontStyle,false));
            }

            swcShowLineNumbers.Active = Setting.TextEditor.ShowLineNumbers;
            swcShowSpaces.Active = Setting.TextEditor.ShowSpaces;
            swcShowVRuler.Active = Setting.TextEditor.ShowVRuler;
            swcShowHRuler.Active = Setting.TextEditor.ShowHRuler;
            swcShowGuidelines.Active = Setting.TextEditor.ShowGuidelines;
            swcShowFold.Active = Setting.TextEditor.ShowFold;
            swcShowIntellisense.Active = Setting.TextEditor.ShowIntellisense;
            swcShowTabs.Active = Setting.TextEditor.ShowTabs;
            swcShowEOLMarkers.Active = Setting.TextEditor.ShowEOLMarkers;
            rdogroupFoldMarkStyle.SelectedIndex = Setting.TextEditor.FoldMarkStyle;
            #endregion

            ThemHelper.SetSystemThem(this);
        }

        /// <summary>
        /// 设置字体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFont_Click(object sender,EventArgs e)
        {
            if(fontDialog.ShowDialog() == DialogResult.OK)
            {
                textEditorControl.Font = fontDialog.Font;
                textEditorControl.Invalidate();
                Setting.TextEditor.FontFamily = fontDialog.Font.FontFamily.Name.ToString();
                Setting.TextEditor.FontSize = fontDialog.Font.Size.ToInt();
                Setting.TextEditor.FontStyle = fontDialog.Font.Style.ToString();
            }
        }


        /// <summary>
        /// 行号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void swcShowLineNumbers_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowLineNumbers = swcShowLineNumbers.Active;
            Setting.TextEditor.ShowLineNumbers = swcShowLineNumbers.Active;
        }


        /// <summary>
        /// 空格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowSpaces_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowSpaces = swcShowSpaces.Active;
            Setting.TextEditor.ShowSpaces = swcShowSpaces.Active;
        }


        /// <summary>
        /// 界线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowVRuler_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowVRuler = swcShowVRuler.Active;
            Setting.TextEditor.ShowVRuler = swcShowVRuler.Active;
        }


        /// <summary>
        /// 顶部标尺
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowHRuler_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowHRuler = swcShowHRuler.Active;
            Setting.TextEditor.ShowHRuler = swcShowHRuler.Active;
        }


        /// <summary>
        /// 辅助线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowGuidelines_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowGuidelines = swcShowGuidelines.Active;
            Setting.TextEditor.ShowGuidelines = swcShowGuidelines.Active;
        }


        /// <summary>
        /// 代码折叠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowFold_MouseClick(object sender,MouseEventArgs e)
        {
            Setting.TextEditor.ShowFold = swcShowFold.Active;
        }



        /// <summary>
        /// 代码提示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowIntellisense_MouseClick(object sender,MouseEventArgs e)
        {
            Setting.TextEditor.ShowIntellisense = swcShowIntellisense.Active;
        }

        /// <summary>
        /// tab
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowTabs_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowTabs = swcShowTabs.Active;
            Setting.TextEditor.ShowTabs = swcShowTabs.Active;
        }

        /// <summary>
        /// 结束符
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void swcShowEOLMarkers_MouseClick(object sender,MouseEventArgs e)
        {
            textEditorControl.ShowEOLMarkers = swcShowEOLMarkers.Active;
            Setting.TextEditor.ShowEOLMarkers = swcShowEOLMarkers.Active;
        }


        /// <summary>
        /// 折叠样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="index"></param>
        /// <param name="text"></param>
        private void rdogroupFoldMarkStyle_ValueChanged(object sender,int index,string text)
        {
            int style = 0;
            switch(text)
            {
                case "Visual Studio":
                    style = 0;
                    break;
                case "PyCharm":
                    style = 1;
                    break;
                case "Idea":
                    style = 2;
                    break;
                case "Sublime":
                    style = 3;
                    break;
                case "Eclipse":
                    style = 4;
                    break;
            }
            Setting.TextEditor.FoldMarkStyle = style;
        }

        
    }
}
